<script type="text/javascript">
$(document).ready(function(){
	 getAllData();
    $("#demoveForm").ajaxForm({
        dataType:  'json',
        success: showResponse
    });

    function showResponse(res) {
        if (res.error) {
            var message = '';
            $.each(res.error,function(i,val){
                $.each(val,function(ii,vv){
                    $("#"+i+"-"+ii).addClass('ui-state-error');
                    $.each(vv,function(iii,vvv){
                        message += $("#"+ii+"-label label").html()+': '+ vvv+ "\n";
                    });
                });
            });
            jAlert(message, '<?php echo $this->translate('checking errors')?>');
        }
        else{
            //$("#outForm input:text").val('').attr('disabled','');
            //$("#out-SaveBtn").attr('disabled','true');
           //$("#demove-CancelBtn").trigger('click');
            getAllData();
            $("#demove-selClass").trigger('change');
        }
        $('#demove-data').unblock();
    }

    $("#demove-SaveBtn").click(function(){
    	$('#demove-data').block({message:'<h1><?php echo $this->translate('Data setting ..')?></h1>',  border: '3px solid #f00'});
        $("#demoveForm").submit();

    });

    $("#demove-CancelBtn").click(function(){
    	$(".ui-state-error").removeClass('ui-state-error ');
    	$("#demove-selClass").val('');
    	$("#demove-studentSn").html('<option value=""><?php echo $this->translate('choose a student')?></option>');
        // 更改為設定模式
    	$("#demoveForm").attr('action','<?php echo $this->route('demove-set')?>');
    	$("#demove-studentSn").attr('disabled','');
        $("#demove-selClass").attr('disabled','');
        $("#demove-toClass").attr('disabled','');
      var d = new Date();
      var curr_date = d.getDate();
      var curr_month = d.getMonth()+1;
      var curr_year = d.getFullYear();
      $("#demove-date").val(curr_year+'-'+curr_month+'-'+curr_date);
      $("#demove-approve_date").val(curr_year+'-'+curr_month+'-'+curr_date);
        });

    $("#demove-selClass").change(function(){
    	var class_id=$(this).val();
        $.get('<?php echo $this->route('getstudents')?>',{class_id:class_id},function(data){
            var ss = '<option value=""><?php echo $this->translate('choose a student')?></option>'+data;
            $("#demove-studentSn").html(ss);
            $("#demove-toClass").empty();
            $("#demove-selClass option").each(function(){
                var val = $(this).val();
                if (val !=''  &&  val.substr(0,1) != class_id.substr(0,1))
                		$(this).clone().appendTo("#demove-toClass");
               });
        });
     });

    $("#demove-zoom-in").click(function(){
        $.openDOMWindow({
            loader:1,
            loaderImagePath:'animationProcessing.gif',
            loaderHeight:16,
            loaderWidth:17,
            width : 960,
            windowSourceID:'#demove-student-list'
        });
    });

    function getAllData(){
        $.get('<?php echo $this->route('get-student')?>',{kind:'demove'},function(data){
            $("#demove-student-list").html(data);
        });
    }
});

// 取回升級資料
// TODO 班級轉換
function setDemoveData(sn) {
	   $.getJSON('<?php echo $this->route('get-transfer-data-byid')?>',{sn:sn},function(data){
	      $.each(data,function(i,v){
	          $("#demove-"+i).val(v);
	      });
	      // 修改 id
	     $("#demove-sn").val(data.sn);
        $(".ui-state-error").removeClass('ui-state-error ');
	      var class_sn = data.grade*100+data.class_sn*1;
	      $("#demove-selClass").val(class_sn);
	    //  $("#demove-selClass").trigger('change');
	      $("#demove-SaveBtn").attr('disabled','');
	      $("#demove-CancelBtn").attr('disabled','');
	      $("#demove-edit-message").html(data.name+'  <?php echo $this->translate('editting..')?>');
	      $("#demove-studentSn").html('');
	      $("#demove-studentSn").append("<option value='"+data.student_sn+"'>"+data.name+"</option>");
	      $("#demove-studentSn").attr('disabled','disabled');
	      $("#demove-selClass").attr('disabled','disabled');
	      $("#demove-toClass").attr('disabled','disabled');

	  });
	}

</script>
<style>
#demove-student-list {overflow: auto;height:350px;width:600px;background:#ffe;border:thin solid #ccc;padding:3px;}
#demove-student-list table {white-space: nowrap;}
</style>
<div id="demove-data">
<div id="demove-dataForm" style="float:left;width:250px">
<form action="<?php echo $this->route('demove-set')?>" method="post"  id="demoveForm" >
<input type="button" id="demove-SaveBtn" value="<?php echo $this->translate('save')?>" />
<input type="button" id="demove-CancelBtn" value="<?php echo $this->translate('cancel')?>" />
<div style="border:#ccc thin solid; padding:5px;margin:5px">
<?php echo $this->form->selClass?>
<?php echo $this->form->studentSn?>
<?php echo $this->form->date?>
<?php echo $this->form->toClass?>
<?php echo $this->form->approve_organ?>
<?php echo $this->form->approve_date?>
<?php echo $this->form->approve_word?>
<?php echo $this->form->approve_number?>
<input type="hidden" name="sn"  id="demove-sn" />
</div>
</form>
</div>

<div id="demove-listTable" style="float:left; margin-left:10px">
<h2><span id="demove-year-semester-span">
<?php echo sprintf($this->translate('%d No. %d academic year semester'), $this->Semester['year'], $this->Semester['semester'])?></span>
 <?php echo $this->translate('all transfer demove data')?>
<img  id="demove-zoom-in" src="<?php echo $this->baseUrl?>/images/zoom-in.png"  style="margin-left:5px" />
</h2>
<div id="demove-student-list"></div>
</div>
<div style="clear:both"></div>
</div>